﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Domain.MainModule.Entities;
using Infraestructure.Data.MainModule.UnitOfWork;
using Infraestructure.CrossCutting.Logging;
using Domain.MainModule.Phones;
using Infraestructure.Data.MainModule.Repositories;

namespace Infraestructure.Data.MainModule.Tests.RepositoriesTests
{
    [TestClass()]
    public class PhoneRepositoryTest : RepositoryTestsBase<Phone>
    {
        public override System.Linq.Expressions.Expression<Func<Phone, bool>> FilterExpression
        {
            get { return a => a.PhoneId == TestConstants.Instance.PHONE_ID; }
        }

        public override System.Linq.Expressions.Expression<Func<Phone, Guid>> OrderByExpression
        {
            get { return a => a.PhoneId; }
        }

        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FindPhone_Invoke_NullSpecThrowNewArgumentNullException_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IPhoneRepository repository = new PhoneRepository(context, traceManager);

            //Act
            repository.FindPhone(null);
        }

        #region PhoneIdSpecification

        [TestMethod()]
        public void FindPhone_Invoke_InvalidPhoneIdReturnNullObject_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IPhoneRepository repository = new PhoneRepository(context, traceManager);

            Guid invalidId = new Guid("11111111-1111-1111-1111-111111111111");
            PhoneIdSpecification spec = new PhoneIdSpecification(invalidId);

            //Act
            Phone phone = repository.FindPhone(spec);

            //Assert
            Assert.IsNull(phone);
        }

        [TestMethod()]
        public void FindPhoneById_Invoke_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IPhoneRepository repository = new PhoneRepository(context, traceManager);

            PhoneIdSpecification spec = new PhoneIdSpecification(TestConstants.Instance.PHONE_ID);

            //Act
            Phone phone = repository.FindPhone(spec);

            //Assert
            Assert.IsNotNull(phone);
        }

        #endregion

        #region PhoneUserIdSpecification

        [TestMethod()]
        public void FindPhone_Invoke_InvalidPhoneUserIdReturnNullObject_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IPhoneRepository repository = new PhoneRepository(context, traceManager);

            Guid invalidId = new Guid("11111111-1111-1111-1111-111111111111");
            PhoneUserIdSpecification spec = new PhoneUserIdSpecification(invalidId);

            //Act
            Phone phone = repository.FindPhone(spec);

            //Assert
            Assert.IsNull(phone);
        }

        [TestMethod()]
        public void FindPhoneByPhoneUserId_Invoke_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IPhoneRepository repository = new PhoneRepository(context, traceManager);

            PhoneUserIdSpecification spec = new PhoneUserIdSpecification(TestConstants.Instance.USER_ID);

            //Act
            IEnumerable<Phone> result = repository.FindPhones(spec);

            //Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count() > 0);
            Assert.IsNotNull(result.First().PhoneType);
        }

        #endregion
    }
}
